 /* 
  * Copyright (C) 2012 maxim 
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */

#include <stdlib.h>
#include <assert.h>

#include "tests.h"
uthread_mutex_t print_mutex;

static void print_to_screen(char* str)
{
	int count = 0,writed = 0;
	int len = strlen(str);
	while (count != len) {
		writed = uthread_write(1,str+count,sizeof(char)*(len-count));
		if (writed	>=	0)
			count += writed;
	}
	return;
}

static char* itoa(int num)
{
	if	(num == 0) 
		return "0";
	int num_digits=0,i=1;
	while (i <= num) {
		num_digits++;
		i *= 10;
	}
	char* str = (char*)malloc((num_digits+1)*sizeof(char));
	char tmp;
	str[num_digits] = '\0';
	for (i = (num_digits-1); i >= 0; i--){
		tmp = (char)(num % 10) + '0';
		num /= 10;
		str[i] = tmp;
	}
	return str;
}

static void block(uthread_mutex_t mutex) 
{
	if (uthread_mutex_lock(mutex) != MUTEX_SUCCESS) {
		print_to_screen("Unable to lock mutex.\n");
		exit(-1);
	}
}

static void unblock(uthread_mutex_t mutex) 
{
	if (uthread_mutex_unlock(mutex) != MUTEX_SUCCESS) {
		print_to_screen("Unable to unlock mutex.\n");
		exit(-1);
	}
}

static inline void delay(int n, char c) {
	int i;
	for (i = 0 ; i < n ; i++)
		i=i;
}

static inline void reset_counter()
{
	counter = 0;
}

static void msg_with_expected_order(int i, char * in, int id){
		block(print_mutex);
		print_to_screen(itoa(i));
	  print_to_screen(": ");
		print_to_screen("Thread Id= ");
		print_to_screen(itoa(id));
		print_to_screen("   ");
		print_to_screen(in);
		print_to_screen("\n");
		unblock(print_mutex);
}

static void msg(char * in){
	msg_with_expected_order(1,in,uthread_self());
}

uthread_mutex_t mutex1;
uthread_mutex_t mutex2;
uthread_mutex_t mutex3;


int thread3(void* arg)
{
	msg_with_expected_order(5, "Thread 3 runs for first time",uthread_self());

	/* can't unlock a lock that belongs to other thread */
	assert (uthread_mutex_unlock(mutex1) == MUTEX_LOCKED);


	assert (uthread_mutex_lock(mutex2) == MUTEX_SUCCESS);
	msg_with_expected_order(6, "Thread 3, got lock2, sleeping",uthread_self());
	uthread_yield();
	int i;
	for(i=0;i<10000000;i++);
	msg_with_expected_order (8, "Thread 3, exit should drop mutex2",uthread_self());

	/* Don't unlock mutex2, it will be unlocked automatically */
	uthread_exit(0);
	assert(0);
}


int thread2(void* arg)
{
	/* take the lock - should be done instantly */

	msg_with_expected_order(3, "Thread 2 runs for first time",uthread_self());
	assert (uthread_mutex_try_lock(mutex1) == MUTEX_SUCCESS);
	msg_with_expected_order (4, "Thread 2, got lock",uthread_self());
	assert (uthread_create(thread3, NULL) == 3);
	uthread_yield();

	msg_with_expected_order(7, "Thread 2 now will sleep for the lock",uthread_self());

	/* Wait for thread 3 */
	assert (uthread_mutex_lock(mutex2) == MUTEX_SUCCESS);

	msg_with_expected_order (9, "Thread 2, got lock 2, sleeping",uthread_self());
	int i;
	for(i=0;i<10000000;i++);
	assert (uthread_mutex_unlock(mutex2) == MUTEX_SUCCESS);

	/* Don't unlock mutex1 - it will be unlocked automatically*/
	return 0;
}

int thread1(void* arg)
{
	msg_with_expected_order(2, "Thread 1 runs first time, creates thread 2",uthread_self());
	assert (uthread_create(thread2, NULL) == 2);
	uthread_yield();

	/* Wait till thread 2 releases the lock
	 * We take lock twice*/

	assert (uthread_mutex_lock(mutex1) == MUTEX_SUCCESS);	
	assert (uthread_mutex_lock(mutex1) == MUTEX_FAILURE);
	msg_with_expected_order(10, "Thread 1, got lock",uthread_self());

	/* Now release it again twice */
	assert (uthread_mutex_unlock(mutex1) == MUTEX_SUCCESS);
	assert (uthread_mutex_unlock(mutex1) == MUTEX_UNLOCKED);

	/* That ought to fail */
	assert (uthread_mutex_unlock(mutex1) == MUTEX_UNLOCKED);

	/* And take all the locks again */
	assert(uthread_mutex_lock(mutex1) == MUTEX_SUCCESS);
	assert(uthread_mutex_lock(mutex2) == MUTEX_SUCCESS);
	assert(uthread_mutex_lock(mutex3) == MUTEX_SUCCESS);

	msg_with_expected_order(11, "Thread 1, exiting",uthread_self());

	return 0;
}

void test13() {

	reset_counter();
	msg("\n***************Running test #13...************");

	assert(uthread_mutex_init(&mutex1) == UTHREAD_SUCCESS);
	assert(uthread_mutex_init(&mutex2) == UTHREAD_SUCCESS);
	assert(uthread_mutex_init(&mutex3) == UTHREAD_SUCCESS);


	msg_with_expected_order(1, "Starting test",uthread_self());
	assert (uthread_create(thread1, NULL) == 1);
	uthread_yield();
	assert (uthread_join(2, NULL) == UTHREAD_SUCCESS);
	assert (uthread_join(1, NULL) == UTHREAD_SUCCESS);

	// to cleanup this thread
	assert (uthread_join(3, NULL) == UTHREAD_SUCCESS);


	assert(uthread_mutex_lock(mutex3) == MUTEX_SUCCESS);
	assert(uthread_mutex_destroy(mutex3) == MUTEX_LOCKED);
	assert (uthread_mutex_unlock(mutex3) == MUTEX_SUCCESS);
	assert(uthread_mutex_destroy(mutex3) == MUTEX_SUCCESS);



}
